
When using UDP, the idea of a "server" and a "client" is not as
clear cut as with TCP. The TCP protocol has a special kind of
socket, ServerSocket, and it the creation of this kind of socket
that makes a process a "server". (Note: Any process can become a
"server. All it has to do is instantiate a ServerSocket object,
bind it to a port, and then call accept(). So a "client" can turn
itself into a "server", which is a fairly common practice.)

With the UDP protocol, there is no special kind of "server socket".
There is only one kind of UDP socket, DatagramSocket. So what do
we mean by a "server" vs a "client" when using UDP?

When using UDP, it is probably better not to use the term "server"
and instead use the idea of a "service". We should say that a
process "provides a udp service" instead of saying that a process
"is a udp server".

What makes a process a "udp service" is that
   1.) the port number it is using is well known to any other
       process that wants to make use of its service,
   2.) immediately after the process has sent a response packet
       to a client process it calls receive() on its DatagramSocket
       so that it can handle another service request from another
       client process.

In summary, a process is a tcp server if it has a ServerSocket using
a well known port number and it calls accept() on the ServerSocket.
A process is a udp service if it has a DatagramSocket on a well known
port number and the process acts like a service on that socket.



Here are the steps used by two processes to communicate with each
other using UDP.


Step 1) A receiving process creates an unbound DatagramSocket object.

                                                 DatagramSocket
                                                +--------------+
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                +--------------+


Step 2) The receiving process "binds" its DatagramSocket object
        to a port number. The port number that it uses is important.
        Any other process that wants to send this process a udp
        packet must know this port number.
        This DatagramSocket is not yet able to receive any packets.

                                                 DatagramSocket
                                                +--------------+
                                                |              |
                                                | Port number  |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                |              |
                                                +--------------+


Step 3) The receiving process attaches to its DatagramSocket object
        a DatagramPacket object that contains a Byte[] object.
        (The Byte[] object will hold the bytes of a received packet.)


                                                 DatagramSocket
                                                +--------------+
                                                |              |
                                                | Port number  |
                                                |              |
                                                |DatagramPacket|
                                                | +----------+ |
                                                | | Byte[]   | |
                                                | |          | |
                                                | |          | |
                                                | |          | |
                                                | +----------+ |
                                                |              |
                                                +--------------+


Step 4) The receiving process calls receive() on its DatagramSocket
        object. The DatagramSocket object can now receive a packet.


Step 5) A sending process creates a DatagramSocket object and
        lets the operating system bind the socket to an ephemeral
        port number.

    DatagramSocket                               DatagramSocket
   +--------------+                             +--------------+
   |              |                             |              |
   | Port number  |                             | Port number  |
   |              |                             |              |
   |              |                             |DatagramPacket|
   |              |                             | +----------+ |
   |              |                             | | Byte[]   | |
   |              |                             | |          | |
   |              |                             | |          | |
   |              |                             | |          | |
   |              |                             | +----------+ |
   |              |                             |              |
   +--------------+                             +--------------+


Step 6) The sending process attaches to its DatagramSocket object
        a DatagramPacket object that contains a Byte[] object
        filled with data (this is the packet of data that will
        be sent to the receiving process).

    DatagramSocket                               DatagramSocket
   +--------------+                             +--------------+
   |              |                             |              |
   | Port number  |                             | Port number  |
   |              |                             |              |
   |DatagramPacket|                             |DatagramPacket|
   | +----------+ |                             | +----------+ |
   | | Byte[]   | |                             | | Byte[]   | |
   | |          | |                             | |          | |
   | |          | |                             | |          | |
   | |          | |                             | |          | |
   | +----------+ |                             | +----------+ |
   |              |                             |              |
   +--------------+                             +--------------+


Step 7) The sending process puts into its DatagramPacket object
        the IP address and port number of the receiving process's
        DatagramSocket (notice that the sending process must be
        aware of the port number being used by the receiving process).


    DatagramSocket                               DatagramSocket
   +--------------+                             +--------------+
   |              |                             |              |
   | Port number  |                             | Port number  |
   |              |                             |              |
   |DatagramPacket|                             |DatagramPacket|
   | +----------+ |                             | +----------+ |
   | | Byte[]   | |                             | | Byte[]   | |
   | |          | |                             | |          | |
   | | IP:Port  | |                             | |          | |
   | |          | |                             | |          | |
   | +----------+ |                             | +----------+ |
   |              |                             |              |
   +--------------+                             +--------------+


Step 8) The sending process calls send() on its DatagramSocket.
        The data from its Byte[] array is sent to the Byte[]
        array of the receiving process's DatagramPacket. The
        receiving DatagramPacket also receives the IP:Port
        pair of the sending process's DatagramSocket (this
        makes it easy for the receiving process to send a
        packet right back to the sending process).


    DatagramSocket                               DatagramSocket
   +--------------+                             +--------------+
   |              |                             |              |
   | Port number  |                             | Port number  |
   |              |                             |              |
   |DatagramPacket|                             |DatagramPacket|
   | +----------+ |                             | +----------+ |
   | | Byte[] ~~>~~~>~~~~~~>~~~~~~~>~~~~~>~~~>~~~~~>Byte[]   | |
   | |          | |                             | |          | |
   | | IP:Port  | |                             | | IP:Port  | |
   | |          | |                             | |          | |
   | +----------+ |                             | +----------+ |
   |              |                             |              |
   +--------------+                             +--------------+


Step 9) The receiving process can call getData() on its
        DatagramPacket object to get the data that was
        sent to it.





In the above pictures we drew the DatagramSocket object,
the DatagramPacket object, and the Byte[] object as if
they were "nested" inside of each other.

    DatagramSocket
   +--------------+
   |              |
   | Port number  |
   |              |
   |DatagramPacket|
   | +----------+ |
   | | Byte[]   | |
   | |          | |
   | | IP:Port  | |
   | |          | |
   | +----------+ |
   |              |
   +--------------+

This being Java, that can never be the case. The correct
picture would look like this, with three completely separate
objects.

    DatagramSocket
   +--------------+
   |              |
   | Port number  |       DatagramPacket       Byte[]
   |              |        +----------+        +-----+
   |              |        | Byte[] ---------->|     |
   |DatagramPacket-------->|          |        +-----+
   |              |        | IP:Port  |        |     |
   +--------------+        |          |        +-----+
                           +----------+        |     |
                                               +-----+
                                               |     |
                                               +-----+
                                               |     |
                                               +-----+
                                               |     |
                                               +-----+
                                               |     |
                                               +-----+
                                               |     |
                                               +-----+
                                               |     |
                                               +-----+

The main implication of this picture is that you can reuse
or replace any one of these objects each time you send or
receive a udp message.